﻿using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Client.ViewModels.Execution
{
    /// <summary>
    /// 工单查看ViewModel
    /// </summary>
    public class LookViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public LookViewModel(ServiceProxy<IExecutionContract> iExecutionContract)
        {
            this._iExecutionContract = iExecutionContract;
        }
        #endregion

        #region 属性
        #region 工单编号
        /// <summary>
        /// 工单编号
        /// </summary>
        [DependencyProperty]
        public string Number { get; set; }
        #endregion

        #region 工时定额
        /// <summary>
        /// 工时定额
        /// </summary>
        [DependencyProperty]
        public int DurationQuota { get; set; }
        #endregion

        #region 实际开始时间
        /// <summary>
        /// 实际开始时间
        /// </summary>
        [DependencyProperty]
        public DateTime? ActualStartTime { get; set; }
        #endregion

        #region 作业计划批号
        /// <summary>
        /// 作业计划批号
        /// </summary>
        [DependencyProperty]
        public string ProductionBatchNo { get; set; }
        #endregion

        #region 是否需质检
        /// <summary>
        /// 是否需质检
        /// </summary>
        [DependencyProperty]
        public bool NeedToInspect { get; set; }
        #endregion

        #region 工厂模型
        /// <summary>
        /// 工厂模型
        /// </summary>
        [DependencyProperty]
        public string FacilityName { get; set; }
        #endregion

        #region 分派班组
        /// <summary>
        /// 分派班组
        /// </summary>
        [DependencyProperty]
        public string TeamName { get; set; }
        #endregion

        #region 异常原因
        /// <summary>
        /// 异常原因
        /// </summary>
        [DependencyProperty]
        public string ExceptionReason { get; set; }
        #endregion

        #region 描述
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 工单名称
        /// <summary>
        /// 工单名称
        /// </summary>
        [DependencyProperty]
        public string Name { get; set; }
        #endregion

        #region 工单状态
        /// <summary>
        /// 工单状态
        /// </summary>
        [DependencyProperty]
        public WorkOrderStatus Status { get; set; }
        #endregion

        #region 实际结束时间
        /// <summary>
        /// 实际结束时间
        /// </summary>
        [DependencyProperty]
        public DateTime? ActualEndTime { get; set; }
        #endregion

        #region 作业计划名称
        /// <summary>
        /// 作业计划名称
        /// </summary>
        [DependencyProperty]
        public string ProductionName { get; set; }
        #endregion

        #region 是否已质检
        /// <summary>
        /// 是否已质检
        /// </summary>
        [DependencyProperty]
        public bool? HasInspected { get; set; }
        #endregion

        #region 工位信息
        /// <summary>
        /// 工位信息
        /// </summary>
        [DependencyProperty]
        public string WorkStationName { get; set; }
        #endregion

        #region 分派人员
        /// <summary>
        /// 分派人员
        /// </summary>
        [DependencyProperty]
        public string PersonName { get; set; }
        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        [DependencyProperty]
        public int Sort { get; set; }
        #endregion

        #region 工艺指导书
        /// <summary>
        /// 工艺指导书
        /// </summary>
        [DependencyProperty]
        public string DirectivePath { get; set; }
        #endregion


        #region 工单人员要求列表
        /// <summary>
        /// 工单人员要求列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderPersonAskInfo> WorkOrderPersonAsks { get; set; }
        #endregion

        #region 工单实际人员列表
        /// <summary>
        /// 工单实际人员列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderActualPersonInfo> WorkOrderActualPersons { get; set; }
        #endregion

        #region 工单设备要求列表
        /// <summary>
        /// 工单设备要求列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderEquipmentAskInfo> WorkOrderEquipmentAsks { get; set; }
        #endregion

        #region 工单实际设备列表
        /// <summary>
        /// 工单实际设备列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderActualEquipmentInfo> WorkOrderActualEquipments { get; set; }
        #endregion

        #region 工单物料要求列表
        /// <summary>
        /// 工单物料要求列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderMaterialAskInfo> WorkOrderMaterialAsks { get; set; }
        #endregion

        #region 工单实际投入物料列表
        /// <summary>
        /// 工单实际投入物料列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderActualMaterialInfo> WorkOrderActualMaterialIns { get; set; }
        #endregion

        #region 工单实际产出物料列表
        /// <summary>
        /// 工单实际产出物料列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderActualMaterialInfo> WorkOrderActualMaterialOuts { get; set; }
        #endregion

        #region 工单参数要求列表
        /// <summary>
        /// 工单参数要求列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderParameterAskInfo> WorkOrderParameterAsks { get; set; }
        #endregion

        #region 工单实际参数列表
        /// <summary>
        /// 工单实际参数列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderActualParameterInfo> WorkOrderActualParameters { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(WorkOrderInfo workOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(WorkOrderInfo workOrderInfo)
        {
            this.DirectivePath = "https://webdoc.lenovo.com.cn/lenovowsi/new_cskb/att/105669/ThinkPad%20T440s%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97V3.0.pdf";
            this.Number = workOrderInfo.Number;
            this.DurationQuota = workOrderInfo.DurationQuota.Hours;
            this.ActualStartTime = workOrderInfo.ActualStartTime;
            this.ProductionBatchNo = workOrderInfo.ExecutionPlanInfo?.ProductionBatchNo;
            this.NeedToInspect = workOrderInfo.NeedToInspect;
            this.FacilityName = workOrderInfo.FacilityInfo?.Name;
            this.TeamName = workOrderInfo.DispatchedTeamInfo?.Name;
            this.ExceptionReason = workOrderInfo.ExceptionReason;
            this.Description = workOrderInfo.Description;
            this.Name = workOrderInfo.Name;
            this.Status = workOrderInfo.Status;
            this.ActualEndTime = workOrderInfo.ActualEndTime;
            this.ProductionName = workOrderInfo.ExecutionPlanInfo?.Name;
            this.HasInspected = workOrderInfo.HasInspected;
            this.WorkStationName = workOrderInfo.WorkStationInfo?.Name;
            this.PersonName = workOrderInfo.DispatchedPersonInfo?.Name;
            this.Sort = workOrderInfo.Sort;

            IExecutionContract executionContract = this._iExecutionContract.Channel;

            //获取工单人员要求
            this.WorkOrderPersonAsks = new ObservableCollection<WorkOrderPersonAskInfo>(await Task.Run(() => executionContract.GetWorkOrderPersonAsks(
               workOrderInfo.Id)));
            //获取工单实际人员
            this.WorkOrderActualPersons = new ObservableCollection<WorkOrderActualPersonInfo>(await Task.Run(() => executionContract.GetWorkOrderActualPersons(
               workOrderInfo.Id)));
            //获取工单设备要求
            this.WorkOrderEquipmentAsks = new ObservableCollection<WorkOrderEquipmentAskInfo>(await Task.Run(() => executionContract.GetWorkOrderEquipmentAsks(
               workOrderInfo.Id)));
            //获取工单实际设备
            this.WorkOrderActualEquipments = new ObservableCollection<WorkOrderActualEquipmentInfo>(await Task.Run(() => executionContract.GetWorkOrderActualEquipments(
               workOrderInfo.Id)));
            //获取工单物料要求
            this.WorkOrderMaterialAsks = new ObservableCollection<WorkOrderMaterialAskInfo>(await Task.Run(() => executionContract.GetWorkOrderMaterialAsks(
                workOrderInfo.Id)));
            //获取工单实际物料投入
            IEnumerable<WorkOrderActualMaterialInfo> workOrderActualMaterialList = await Task.Run(() => executionContract.GetWorkOrderActualMaterials(
                 workOrderInfo.Id));
            this.WorkOrderActualMaterialIns = new ObservableCollection<WorkOrderActualMaterialInfo>(workOrderActualMaterialList.Where(x => x.DirectionType == MaterialDirectionType.Input));
            //获取工单实际物料产出
            this.WorkOrderActualMaterialOuts = new ObservableCollection<WorkOrderActualMaterialInfo>(workOrderActualMaterialList.Where(x => x.DirectionType == MaterialDirectionType.Output));
            //获取工单参数要求
            this.WorkOrderParameterAsks = new ObservableCollection<WorkOrderParameterAskInfo>(await Task.Run(() => executionContract.GetWorkOrderParameterAsks(
              workOrderInfo.Id)));
            //获取工单实际参数
            this.WorkOrderActualParameters = new ObservableCollection<WorkOrderActualParameterInfo>(await Task.Run(() => executionContract.GetWorkOrderActualParameters(
               workOrderInfo.Id)));
        }
        #endregion
        #endregion
    }
}
